package com.dingwei.superiordiary.mvp.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.MoreReplyBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.LoginActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class MoreReplyGoodsPresenter extends basePresenter {

    private Contract.MoreReplyView moreReplyView;
    private Dialog bottomDialog;
    public MoreReplyGoodsPresenter(Contract.MoreReplyView view) {
        super(view);
        this.moreReplyView = view;
    }

    public void showEvaluationDialog(final String id, final String pid) {

        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_evaluation, null);
        final EditText etEvaluation = (EditText) view.findViewById(R.id.et_evaluation);
        Button btnAddComment = (Button) view.findViewById(R.id.btn_add_comment);
        btnAddComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                saveComment(id,pid,etEvaluation.getText().toString());
                bottomDialog.dismiss();
            }
        });
        view.measure(0, 0);
        bottomDialog = new Dialog(BaseActivity.context, R.style.Theme_Light_Dialog);
        bottomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        bottomDialog.setContentView(view);
        Window dialogWindow = bottomDialog.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        bottomDialog.show();
    }


    private void saveComment(String id, final String pid, final String content) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.PRODUCT_SAVE_COMMENT);
        rp.addBodyParameter("id",id);
        rp.addBodyParameter("pid",pid);
        rp.addBodyParameter("content",content);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());

        x.http().post(rp, new myCallback(BaseActivity.context,MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                moreReplyView.replySuccess(content);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    public void initData(String pid, int page, final boolean isRefresh, boolean isShowDialog) {

        RequestParams rp =new RequestParams(URLS.HOST+URLS.PRODUCT_SUB_COMMENT_LIST);
        rp.addBodyParameter("id",pid);
        rp.addBodyParameter("page",page+"");

        x.http().post(rp, new myCallback(BaseActivity.context, MoreReplyBean.class,isShowDialog,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                MoreReplyBean bean = (MoreReplyBean) baseBean;
                if (isRefresh){
                    moreReplyView.OnRefresh(isRefresh,bean);
                }else {
                    moreReplyView.OnLoadMore(isRefresh,bean);
                }

            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                moreReplyView.onFail();
            }
        });
    }

    public void like(String id, String pid) {

        if (!BaseActivity.value.getIsLogin()){
            Intent intent = new Intent(BaseActivity.context,LoginActivity.class);
            BaseActivity.context.startActivity(intent);
            return;
        }
        RequestParams rp = new RequestParams(URLS.HOST+URLS.PRODUCT_LIKES_COMMENT);
        rp.addBodyParameter("id",pid);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());

        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast("点赞成功");
                moreReplyView.likeSuccess();
            }
            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }

    public void showReportDialog(Activity activity) {
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_choose_photo, null);

        TextView tv_photograph = (TextView) view.findViewById(R.id.tv_photograph);
        TextView tv_album = (TextView) view.findViewById(R.id.tv_album);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_photograph.setText("举报");
        tv_album.setText("屏蔽此评论");
        tv_album.setVisibility(View.GONE);
        tv_photograph.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
                //举报
                moreReplyView.Report();
            }
        });
        tv_album.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
                //屏蔽此评论
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
            }
        });

        bottomDialog = new Dialog(activity, R.style.Theme_Light_Dialog);
        bottomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        bottomDialog.setContentView(view);
        Window dialogWindow = bottomDialog.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        bottomDialog.show();
    }
}
